package PersistenciaDAOs;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import exceptions.PersistenciaException;
import SQLQrys.QrysMercante;
import SQLQrys.QrysParametros;
import logica.ConexionBD;
import logica.Mercante;

public class DAOMercante {
			
		private ConexionBD DBC;
		private DAOChorro daoC;
		
		public DAOMercante(){
			
		}
		
		public void insert(Mercante Mer) throws PersistenciaException{
			String ins=null;
			try {
				
				int chor;
				QrysMercante con = new QrysMercante();
				ins=con.insertMercante();
				PreparedStatement pstmt1;
				DBC=new ConexionBD();
				pstmt1 = DBC.getConnection().prepareStatement(ins);
				
				if(Mer.isChorrosEncendidos())
					chor = 1;
				else 
					chor = 0;
				
				pstmt1.setInt(1, Mer.getIdPartida());
				pstmt1.setInt(2, Mer.getPtoInicioX());
				pstmt1.setInt(3, Mer.getPtoInicioY());
				pstmt1.setInt(4, Mer.getPtoFinX());
				pstmt1.setInt(5, Mer.getPtoFinY());
				pstmt1.setInt(5, chor);
				
				pstmt1.executeUpdate();
				//cierro preparedStatement.
				pstmt1.close();
				
			} catch (SQLException e) {
				e.printStackTrace();
				throw new PersistenciaException("Se ha producido un error al intentar guardar el Mercante");
			}finally{
				DBC.desconectar();
			}	
		}
		
		public Mercante select(int idPart){
			String sel=null;
			String selPar=null;
			
			Mercante Mer = new Mercante();
			
			try{
				QrysMercante cons=new QrysMercante();
				QrysParametros consPar=new QrysParametros();
				
				sel = cons.selectMercante();
				selPar = consPar.getParametros();

				
				//creo preparedStatement, seteo parametros y ejecuto la consulta.
				PreparedStatement pstmt1;
				PreparedStatement pstmt2;

				DBC=new ConexionBD();

				pstmt1 = DBC.getConnection().prepareStatement(sel);
				pstmt1.setInt(1, idPart);
				
				pstmt2 = DBC.getConnection().prepareStatement(selPar);

				ResultSet rs = pstmt1.executeQuery();
				while(rs.next()){
					Mer.setIdPartida(rs.getInt("id_partida"));
					Mer.setPtoInicioX(rs.getInt("pto_ini_x"));
					Mer.setPtoInicioY(rs.getInt("pto_ini_y"));
					Mer.setPtoFinX(rs.getInt("pto_fin_x"));
					Mer.setPtoFinY(rs.getInt("pto_fin_y"));
					
					if(rs.getInt("chorro_on") == 1)
						Mer.setChorrosEncendidos(true);
					else
						Mer.setChorrosEncendidos(false);
				}
				
				ResultSet rsPar = pstmt2.executeQuery();
				while(rsPar.next()){
					Mer.setAlcance(rsPar.getInt("velocidad"));
					Mer.setVelocidad(rsPar.getInt("alcance"));
					Mer.setVisibilidad(rsPar.getInt("visibilidad"));
					
				}
				
				Mer.setChorros(daoC.select(idPart));
				
				rs.close();
				rsPar.close();
				pstmt1.close();
				pstmt2.close();
		}
			catch (SQLException e) {
				e.printStackTrace();
				throw new PersistenciaException("Se ha producido un error al intentar levantar el mercante");
				}finally{
					DBC.desconectar();
					return Mer;
					}	
				}
		}

